const router = require('koa-router')();
const data = new (require('../data')).data();
const multer = require('koa-multer')
const axios = require('axios');
const config = require('../config');
router.prefix('/api');

var imgURL = "http://www.eocpharma.com:8084";

//加载配置

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'public/img/')
    },
    filename: function (req, file, cb) {
        var fileArray = file.originalname.split('.');
        var hz = fileArray[fileArray.length - 1];
        cb(null, 'mt_' + parseInt(Math.random() * 100) + '_' + Date.now() + '.' + hz);
    }
})

var upload = multer({ storage: storage });

router.get('/companyIntroduction', async (ctx, next) => {
    var sql;
    var res;
    sql = "select text,title from companyIntroduction limit 1"
    res = await data.query(sql);
    var title = res[0].title;
    var text = res[0].text;
    ctx.body = data.setStatusJson(true, { apiName: 'companyIntroduction_query', data: { title: title, text: text } })
});
router.post('/companyIntroduction', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var title = bodyData.title;
    var text = bodyData.text;
    var sql;
    var res;
    sql = "update companyIntroduction set `text` = ?,`title` = ? limit 1";
    res = await data.query(sql, [text, title]);
    ctx.body = data.setStatusJson(true, { apiName: 'companyIntroduction_update', data: {} })
})
router.get('/managementTeam', async (ctx, next) => {
    var sql;
    var res;
    sql = "select `id`,name,img,position,introduce from managementTeam";
    res = await data.query(sql);
    ctx.body = data.setStatusJson(true, { apiName: 'managementTeam_query', data: res });
})
router.post('/managementTeam', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var name = bodyData.name;
    var img = bodyData.img;
    var position = bodyData.position;
    var introduce = bodyData.introduce;
    var sql;
    var res;
    sql = "insert into managementTeam(`name`,`img`,`position`,`introduce`) values(?,?,?,?)";
    await data.query(sql, [name, img, position, introduce]);
    ctx.body = data.setStatusJson(true, { apiName: 'managementTeam_add', data: {} });
})
router.delete('/managementTeam/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var res, sql;
    sql = "delete from managementTeam where id = ?";
    res = await data.query(sql, [id]);
    ctx.body = data.setStatusJson(true, { apiName: 'managementTeam_delete', data: {} });
})
router.post('/imgUpload', upload.single('file'), async (ctx, next) => {
    var file = ctx.req.file;
    ctx.body = data.setStatusJson(true, { apiName: 'imgUpload', data: { filename: file.filename } });

})
router.post('/imgUploadEditor', upload.single('file'), async (ctx, next) => {
    var file = ctx.req.file;
    console.log(imgURL + "/public/img/" + file.filename);
    ctx.body = {
        error: false,
        path: imgURL + "/public/img/" + file.filename
    }
})
router.get('/progressManagement', async (ctx, next) => {
    var sql;
    var res;
    sql = "select id,`name_a`,`name_b`,`cr`,`target`,`cImg`,`introduce` from object";
    res = await data.query(sql);
    for (var i = 0; i < res.length; i++) {
        res[i].name = res[i].name_a + res[i].name_b;
        var id = res[i].id;
        var res2;
        sql = "select region,stage from object_stage where object_id = ?";
        res2 = await data.query(sql, [id]);
        var region = [];
        var stage = [];
        for (var j = 0; j < res2.length; j++) {
            region.push(res2[j].region);
            stage.push(res2[j].stage);
        }
        res[i].region = region;
        res[i].stage = stage;
    }
    ctx.body = data.setStatusJson(true, { apiName: 'progressManagement_query', data: res });
})
router.post('/progressManagement', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var name_a = bodyData.name_a;
    var name_b = bodyData.name_b;
    var cr = bodyData.cr;
    var items = bodyData.items;
    var img = bodyData.img;
    var target = bodyData.target;
    var introduce = bodyData.introduce;
    var sql;
    var res;
    sql = "insert into object(`name_a`,`name_b`,`cr`,cImg,target,introduce) values(?,?,?,?,?,?);select LAST_INSERT_ID();";
    res = await data.query(sql, [name_a, name_b, cr, img, target, introduce]);
    //console.log(res);
    var insert_id = res[1][0]['LAST_INSERT_ID()'];
    for (var i = 0; i < items.length; i++) {
        sql = "insert into object_stage(`object_id`,`region`,`stage`) values(?,?,?)";
        await data.query(sql, [insert_id, items[i].region, items[i].stage]);
    }
    ctx.body = data.setStatusJson(true, { apiName: 'progressManagement_add', data: { insert_id: insert_id } });
})
router.put('/progressManagement', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var id = bodyData.id;
    var name_a = bodyData.name_a;
    var name_b = bodyData.name_b;
    var cr = bodyData.cr;
    var items = bodyData.items;
    var img = bodyData.img;
    var target = bodyData.target;
    var introduce = bodyData.introduce;
    var sql;
    var res;
    sql = "update object set `name_a` = ?,`name_b` = ?,`cr` = ?,cImg = ?,target = ?,introduce = ? where id = ?";
    await data.query(sql, [name_a, name_b, cr, img, target, introduce, id]);
    sql = "delete from object_stage where object_id = ?";
    await data.query(sql, [id]);
    for (var i = 0; i < items.length; i++) {
        sql = "insert into object_stage(`object_id`,`region`,`stage`) values(?,?,?)";
        await data.query(sql, [id, items[i].region, items[i].stage]);
    }
    ctx.body = data.setStatusJson(true, { apiName: 'progressManagement_update', data: {} });
})
router.delete('/progressManagement/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var res, sql;
    sql = "delete from object where id = ?";
    await data.query(sql, [id]);
    sql = "delete from object_stage where object_id = ?";
    await data.query(sql, [id]);
    ctx.body = data.setStatusJson(true, { apiName: 'progressManagement_delete', data: {} });
})
router.get('/ceoMessage', async (ctx, next) => {
    var sql;
    var res;
    sql = "select text,title from ceoMessage limit 1"
    res = await data.query(sql);
    var title = res[0].title;
    var text = res[0].text;
    ctx.body = data.setStatusJson(true, { apiName: 'ceoMessage_query', data: { title: title, text: text } })
})
router.post('/ceoMessage', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var title = bodyData.title;
    var text = bodyData.text;
    var sql;
    var res;
    sql = "update ceoMessage set `text` = ?,`title` = ? limit 1";
    res = await data.query(sql, [text, title]);
    ctx.body = data.setStatusJson(true, { apiName: 'ceoMessage_update', data: {} })
})
router.get('/companyVision', async (ctx, next) => {
    var sql;
    var res;
    sql = "select text,title from companyVision limit 1"
    res = await data.query(sql);
    var title = res[0].title;
    var text = res[0].text;
    ctx.body = data.setStatusJson(true, { apiName: 'companyVision_query', data: { title: title, text: text } })
})
router.post('/companyVision', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var title = bodyData.title;
    var text = bodyData.text;
    var sql;
    var res;
    sql = "update companyVision set `text` = ?,`title` = ? limit 1";
    res = await data.query(sql, [text, title]);
    ctx.body = data.setStatusJson(true, { apiName: 'companyVision_update', data: {} })
})
router.get('/scientificConsultant', async (ctx, next) => {
    var sql;
    var res;
    sql = "select `id`,`name`,img,position,introduce from scientificConsultant";
    res = await data.query(sql);
    ctx.body = data.setStatusJson(true, { apiName: 'scientificConsultant_query', data: res });
})
router.post('/scientificConsultant', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var name = bodyData.name;
    var img = bodyData.img;
    var position = bodyData.position;
    var introduce = bodyData.introduce;
    var sql;
    var res;
    sql = "insert into scientificConsultant(`name`,`img`,`position`,`introduce`) values(?,?,?,?)";
    await data.query(sql, [name, img, position, introduce]);
    ctx.body = data.setStatusJson(true, { apiName: 'scientificConsultant_add', data: {} });
})
router.delete('/scientificConsultant/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var res, sql;
    sql = "delete from scientificConsultant where id = ?";
    res = await data.query(sql, [id]);
    ctx.body = data.setStatusJson(true, { apiName: 'scientificConsultant_delete', data: {} });
})
router.get('/investor', async (ctx, next) => {
    var sql;
    var res;
    sql = "select `id`,`name`,img,position,introduce,`link` from investor";
    res = await data.query(sql);
    ctx.body = data.setStatusJson(true, { apiName: 'investor_query', data: res });
})
router.post('/investor', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var name = bodyData.name;
    var img = bodyData.img;
    var position = bodyData.position;
    var introduce = bodyData.introduce;
    var link = bodyData.link;
    var sql;
    var res;
    sql = "insert into investor(`name`,`img`,`position`,`introduce`,`link`) values(?,?,?,?,?)";
    await data.query(sql, [name, img, position, introduce, link]);
    ctx.body = data.setStatusJson(true, { apiName: 'investor_add', data: {} });
})
router.delete('/investor/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var res, sql;
    sql = "delete from investor where id = ?";
    res = await data.query(sql, [id]);
    ctx.body = data.setStatusJson(true, { apiName: 'investor_delete', data: {} });
})
router.get('/cooperativePartner', async (ctx, next) => {
    var sql;
    var res;
    sql = "select `id`,`imgName`,`text`,`link` from cooperativePartner";
    res = await data.query(sql);
    ctx.body = data.setStatusJson(true, { apiName: 'cooperativePartner_query', data: res });
})
router.post('/cooperativePartner', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var img = bodyData.img;
    var text = bodyData.text;
    var link = bodyData.link;
    var sql;
    var res;
    sql = "insert into cooperativePartner(`imgName`,`text`,`link`) values(?,?,?);select LAST_INSERT_ID();";
    res = await data.query(sql, [img, text, link]);
    var insert_id = res[1][0]['LAST_INSERT_ID()'];
    ctx.body = data.setStatusJson(true, { apiName: 'cooperativePartner_add', data: { insert_id: insert_id } })
})
router.delete('/cooperativePartner/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var res, sql;
    sql = "delete from cooperativePartner where id = ?";
    res = await data.query(sql, [id]);
    ctx.body = data.setStatusJson(true, { apiName: 'cooperativePartner_delete', data: {} });
})
router.get('/banner', async (ctx, next) => {
    var sql;
    var res;
    sql = "select `id`,`location`,`name`,`img`,`link` from banner";
    res = await data.query(sql);
    var resData = (await axios.get('http://localhost:' + config.port + '/api/type')).data.message.data;
    for (var i = 0; i < res.length; i++) {
        for (var j = 0; j < resData.length; j++) {
            if (res[i].location == resData[j].id) {
                res[i].location = resData[j].value;
                break;
            }
        }
    }
    ctx.body = data.setStatusJson(true, { apiName: 'banner_query', data: res })
})
router.get('/getbackgourd', async (ctx, next) => {
    var sql, res;
    var data2 = [[], [], [], [], [], []];
    sql = "select `id`,`location`,`name`,`img`,`link` from banner";
    res = await data.query(sql);
    for (var i = 0; i < res.length; i++) {
        data2[res[i].location - 1].push(res[i]);
    }
    ctx.body = data.setStatusJson(true, { apiName: 'getbackgourd', data: data2 });
})
router.post('/banner', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var location = bodyData.location;
    var name = bodyData.name;
    var img = bodyData.img;
    var link = bodyData.link;
    var sql;
    var res;
    sql = "insert into banner(`location`,`name`,`img`,`link`) values(?,?,?,?);select LAST_INSERT_ID();";
    res = await data.query(sql, [location, name, img, link]);
    var insert_id = res[1][0]['LAST_INSERT_ID()'];
    ctx.body = data.setStatusJson(true, { apiName: 'banner_add', data: { insert_id: insert_id } });
})
router.delete('/banner/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var res, sql;
    sql = "delete from banner where id = ?";
    res = await data.query(sql, [id]);
    ctx.body = data.setStatusJson(true, { apiName: 'banner_delete', data: {} });
})
router.get('/type', async (ctx, next) => {
    var sql, res;
    sql = "select `id`,`value` from `type`";
    res = await data.query(sql);
    ctx.body = data.setStatusJson(true, { apiName: 'type_query', data: res });
})
router.get('/cooperationMode', async (ctx, next) => {
    var sql, res;
    sql = "select text,title from `cooperationMode` limit 1"
    res = await data.query(sql);
    var title = res[0].title;
    var text = res[0].text;
    ctx.body = data.setStatusJson(true, { apiName: 'cooperationMode_query', data: { title: title, text: text } })
})
router.post('/cooperationMode', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var title = bodyData.title;
    var text = bodyData.text;
    var sql;
    var res;
    sql = "update cooperationMode set `text` = ?,`title` = ? limit 1";
    res = await data.query(sql, [text, title]);
    ctx.body = data.setStatusJson(true, { apiName: 'cooperationMode_update', data: {} })
})
router.get('/development', async (ctx, next) => {
    var sql, res;
    sql = "select text,title from `development` limit 1"
    res = await data.query(sql);
    var title = res[0].title;
    var text = res[0].text;
    ctx.body = data.setStatusJson(true, { apiName: 'development_query', data: { title: title, text: text } })
})
router.post('/development', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var title = bodyData.title;
    var text = bodyData.text;
    var sql;
    var res;
    sql = "update development set `text` = ?,`title` = ? limit 1";
    res = await data.query(sql, [text, title]);
    ctx.body = data.setStatusJson(true, { apiName: 'development_update', data: {} })
})
router.get('/production', async (ctx, next) => {
    var sql, res;
    sql = "select text,title from `production` limit 1"
    res = await data.query(sql);
    var title = res[0].title;
    var text = res[0].text;
    ctx.body = data.setStatusJson(true, { apiName: 'production_query', data: { title: title, text: text } })
})
router.post('/production', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var title = bodyData.title;
    var text = bodyData.text;
    var sql;
    var res;
    sql = "update production set `text` = ?,`title` = ? limit 1";
    res = await data.query(sql, [text, title]);
    ctx.body = data.setStatusJson(true, { apiName: 'production_update', data: {} })
})
router.get('/clinical', async (ctx, next) => {
    var sql, res;
    sql = "select text,title from `clinical` limit 1"
    res = await data.query(sql);
    var title = res[0].title;
    var text = res[0].text;
    ctx.body = data.setStatusJson(true, { apiName: 'clinical_query', data: { title: title, text: text } })
})
router.post('/clinical', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var title = bodyData.title;
    var text = bodyData.text;
    var sql;
    var res;
    sql = "update clinical set `text` = ?,`title` = ? limit 1";
    res = await data.query(sql, [text, title]);
    ctx.body = data.setStatusJson(true, { apiName: 'clinical_update', data: {} })
})
router.get('/news_/:pageIndex/:pageCount', async (ctx, next) => {
    ctx.set("Cache-Control", "no-cache");
    var pageIndex = ctx.params.pageIndex;
    var pageCount = ctx.params.pageCount;
    var sql, res;
    sql = "select count(*) as count from news limit 1";
    res = await data.query(sql);
    var dataCount = res[0]['count'];
    sql = "select text,title,`id`,`createTime` from news limit " + (pageIndex - 1) * pageCount + "," + pageCount;
    res = await data.query(sql);
    for (var i = 0; i < res.length; i++) {
        var date = new Date(res[i].createTime);
        res[i].createTime = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
    }
    var data2 = { rows: res, count: dataCount };
    ctx.body = data.setStatusJson(true, { apiName: 'news_query', data: data2 })
})
router.delete('/news/:newsid', async (ctx, next) => {
    var sql, res;
    var newsid = ctx.params.newsid;
    sql = "delete from news where id = ?";
    await data.query(sql, [newsid]);
    ctx.body = data.setStatusJson(true, { apiName: 'news_delete', data: {} });
})
router.post('/news', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var sql, res;
    var title = bodyData.title;
    var createTime = bodyData.createTime;
    var object_id = bodyData.objid;
    var text = bodyData.text;
    sql = "insert into news(`title`,`createTime`,`object_id`,`text`) values(?,?,?,?)";
    await data.query(sql, [title, createTime, object_id, text]);
    ctx.body = data.setStatusJson(true, { apiName: 'news_update', data: {} })
})
router.get('/news/:pageIndex/:pageCount', async (ctx, next) => {
    ctx.set("Cache-Control", "no-cache");
    var pageIndex = ctx.params.pageIndex;
    var pageCount = ctx.params.pageCount;
    var sql, res;
    //sql = "select count(*) as count from news where object_id = 0 limit 1";
    sql = "select count(*) as count from news limit 1";
    res = await data.query(sql);
    var dataCount = res[0]['count'];
    //sql = "select `text`,`title`,`id`,`createTime` from news where object_id = 0 limit " + (pageIndex - 1) * pageCount + "," + pageCount;
    sql = "select `text`,`title`,`id`,`createTime` from news limit " + (pageIndex - 1) * pageCount + "," + pageCount;
    res = await data.query(sql);
    for (var i = 0; i < res.length; i++) {
        var stringArray = (res[i].createTime.toString()).split(" ");
        res[i].month = stringArray[1];
        res[i].day = stringArray[2];
        res[i].year = stringArray[3];
        delete res[i].createTime;
    }
    var data2 = { rows: res, count: dataCount };
    ctx.body = data.setStatusJson(true, { apiName: 'news_query', data: data2 })
})
router.get('/news/:objid', async (ctx, next) => {
    var objid = ctx.params.objid;
    var res, sql;
    sql = "select `text`,`title`,`id`,`createTime` from news where object_id = ?";
    res = await data.query(sql, [objid]);
    for (var i = 0; i < res.length; i++) {
        var stringArray = (res[i].createTime.toString()).split(" ");
        res[i].month = stringArray[1];
        res[i].day = stringArray[2];
        res[i].year = stringArray[3];
        delete res[i].createTime;
    }
    ctx.body = data.setStatusJson(true, { apiName: 'news_objectid_query', data: res });
})
router.get('/ObjText/:objId', async (ctx, next) => {
    ctx.set("Cache-Control", "no-cache");
    var objId = ctx.params.objId;
    var res, sql;
    sql = "select `introduce` from object where id = ? limit 1";
    res = await data.query(sql, [objId]);
    ctx.body = data.setStatusJson(true, { apiName: 'ObjText_query', data: res[0].introduce });
})
router.get('/contactUs', async (ctx, next) => {
    var res, sql;
    sql = "select `id`,`img`,`address`,`phone`,`site`,`link` from contactUs";
    res = await data.query(sql);
    ctx.body = data.setStatusJson(true, { apiName: 'contactUs_query', data: res });
})
router.post('/contactUs', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var address = bodyData.address;
    var phone = bodyData.phone;
    var img = bodyData.img;
    var site = bodyData.site;
    var link = bodyData.link;
    var sql;
    var res;
    sql = "insert into contactUs(`address`,`phone`,`img`,`site`,`link`) values(?,?,?,?,?);select LAST_INSERT_ID();";
    res = await data.query(sql, [address, phone, img, site, link]);
    var insert_id = res[1][0]['LAST_INSERT_ID()'];
    ctx.body = data.setStatusJson(true, { apiName: 'contactUs_add', data: { insert_id: insert_id } })
})
router.delete('/contactUs/:id', async (ctx, next) => {
    var id = ctx.params.id;
    var res, sql;
    sql = "delete from contactUs where id = ?";
    res = await data.query(sql, [id]);
    ctx.body = data.setStatusJson(true, { apiName: 'contactUs_delete', data: {} });
})
router.get('/indexText', async (ctx, next) => {
    var sql, res;
    sql = "select `value` from indexText limit 1";
    res = await data.query(sql);
    ctx.body = data.setStatusJson(true, { apiName: 'indexText_query', data: res[0] })
})
router.post('/indexText', async (ctx, next) => {
    var bodyData = ctx.request.body.data;
    var value = bodyData.value;
    var sql, res;
    sql = "update indexText set `value` = ?";
    await data.query(sql, [value]);
    ctx.body = data.setStatusJson(true, { apiName: 'indexText_update', data: {} });
})
module.exports = router;